package com.example.md;

import java.io.File;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class Application {

    private static String mdDir = "/root/workspace/Markdown"; // 默认值
    private static String port = "8888"; // 默认值

    public static void main(String[] args) {
        // 解析命令行参数
        for (String arg : args) {
            if (arg.startsWith("-md_dir=")) {
                mdDir = arg.substring(8); // 截取参数值
            }
            if (arg.startsWith("-port=")) {
                port = arg.substring(6); // 截取参数值
            }
        }

        // 将mdDir设置到系统属性，以便在application.yaml中引用
        System.setProperty("md_dir", mdDir);
        System.setProperty("server.port", port);

        SpringApplication.run(Application.class, args);
    }

    // 创建文件夹的Bean
    @Bean
    public CommandLineRunner init() {
        return args -> {
            File dir = new File(mdDir);
            if (!dir.exists()) {
                if (dir.mkdirs()) {
                    System.out.println("成功创建目录: " + mdDir);
                } else {
                    System.err.println("创建目录失败: " + mdDir);
                }
            } else {
                System.out.println("目录已存在: " + mdDir);
            }
        };
    }
}
